<!-- 考核标准编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改考核标准':'添加考核标准'"
    :visible="visible"
    width="850px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">
      <el-row :gutter="15">
      <el-col :sm="12">
      <el-form-item
        label="考核标准:"
        prop="name">
        <el-input
          :maxlength="20"
          v-model="form.name"
          placeholder="请输入考核标准"
          clearable/>
      </el-form-item>
      </el-col>
      <el-col :sm="12">
      <el-form-item label="所属学校:" prop="organization">
          <el-select
            filterable
            clearable
            v-model="form.organization"
            @change="getProjectList"
            size="small"
            placeholder="-请选择所属学校-"
            class="ele-block">
            <el-option v-for="item in schoolList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
      </el-form-item>
      <el-form-item label="所属项目:" prop="project">
          <el-select
            filterable
            clearable
            v-model="form.project"
            @change="$forceUpdate()"
            size="small"
            placeholder="-请选择所属项目-"
            class="ele-block">
            <el-option v-for="item in projectList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
      </el-form-item>
      </el-col>
      <!-- <el-col :sm="8">
      <el-form-item label="所属院系:" ref="superior" prop="superior">
          <el-select
            filterable
            clearable
            v-model="form.superior"
            size="small"
            placeholder="-请选择院系-"
            class="ele-block">
            <el-option v-for="item in departmentsList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
      </el-form-item>
      </el-col> -->
      </el-row>
      <el-row :gutter="15">
      <el-form-item label="备注:">
        <el-input
          :rows="3"
          clearable
          type="textarea"
          :maxlength="200"
          v-model="form.note"
          placeholder="请输入备注"/>
      </el-form-item>
      </el-row>
      <div style="margin: 6px 0 5px 0;">
        <el-header>国家体质健康监测考核标准</el-header>
      </div>
      <el-row justify="center" align="middle" style="height: 2vw;">
          <el-col style="text-align: center;" :sm="1">
          序号
          </el-col>
          <el-col style="text-align: center;" :sm="3">
            性别
          </el-col>
          <el-col style="text-align: center;" :sm="3">
            计算
          </el-col>
          <el-col style="text-align: center;" :sm="6">
            目标值
          </el-col>
          <el-col style="text-align: center;" :sm="3">
            单位
          </el-col>
          <el-col style="text-align: center;" :sm="3">
            考核等级
          </el-col>
          <el-col style="text-align: center;" :sm="3">
            考核分数
          </el-col>
          <el-col style="text-align: center;" :sm="2">
          操作
          </el-col>
      </el-row>
      <el-row :gutter="15"
       v-for="(item, index) in ruleForms"
              :key="index"
              style="margin-left: 0.5vw"
              >
      <el-col :sm="1">
        <el-form-item label-width="0">
          {{index+1}}
          </el-form-item>
        </el-col>
      <el-col :sm="3">
          <el-form-item label-width="0">
            <el-select
              clearable
              class="ele-block"
              v-model="item.sex"
              placeholder="请选择类型">
                      <el-option label="男" :value="1"/>
                      <el-option label="女" :value="2"/>
                    </el-select>
          </el-form-item>
        </el-col>
        <el-col :sm="3">
          <el-form-item label-width="0">
              <el-select
                 v-model="item.conditions"
                 placeholder="请选择"
                 >
                  <el-option label="大于" value="gt"></el-option>
                  <el-option label="大于等于" value="egt"></el-option>
                  <el-option label="小于" value="lt"></el-option>
                  <el-option label="小于等于" value="elt"></el-option>
                  <el-option label="范围" value="between"></el-option>
              </el-select>
          </el-form-item>
        </el-col>
        <el-col :sm="6">
            <el-form-item label-width="0">
              <el-col :sm="12">
                <el-input
                  v-model="item.target[0]"
                  placeholder="请输入目标值"
                  type="number"
                  ></el-input>
                </el-col>
                <el-col :sm="12">
                <el-input
                  v-model="item.target[1]"
                  v-show="item.conditions=='between'"
                  placeholder="请输入目标值"
                  type="number"
                  ></el-input>
                  </el-col>
            </el-form-item>
        </el-col>
        <el-col :sm="3" label-width="0">
            <el-select
               v-model="item.unit"
               @change="changelevel(item.unit)"
               placeholder="请选择"
               >
               <el-option v-for="item2 in staunit" :label="item2.label" :value="item2.value"/>
            </el-select>
        </el-col>
        <el-col :sm="3">
          <el-form-item label-width="0">
            <el-block v-if="item.unit=='kg/m^2'">
              <el-select
                 v-model="item.level"
                 placeholder="请选择"
                 >
                 <el-option v-for="item2 in stalevel2" :key="item2.value" :label="item2.label" :value="item2.value"/>
                  <!-- <el-option label="优秀" value="1"></el-option>
                  <el-option label="良好" value="2"></el-option>
                  <el-option label="及格" value="3"></el-option>
                  <el-option label="不及格" value="0"></el-option> -->
              </el-select>
            </el-block>
            <el-block v-if="item.unit!='kg/m^2'">
              <el-select
                 v-model="item.level"
                 placeholder="请选择"
                 >
                 <el-option v-for="item2 in stalevel1" :key="item2.value" :label="item2.label" :value="item2.value"/>
                  <!-- <el-option label="优秀" value="1"></el-option>
                  <el-option label="良好" value="2"></el-option>
                  <el-option label="及格" value="3"></el-option>
                  <el-option label="不及格" value="0"></el-option> -->
              </el-select>
            </el-block>
          </el-form-item>
        </el-col>
        <el-col :sm="3">
          <el-form-item label-width="0">
              <el-input
                v-model="item.score"
                placeholder="请输入考核分数"
                type="number"
                ></el-input>
          </el-form-item>
        </el-col>
        <el-col :sm="2">
              <i
                 class="el-icon-circle-plus-outline"
                 type="danger"
                 v-if="index == 0"
                 style="font-size: 30px; color: #67c23a; margin-right: 5px"
                 @click="addItem"
                 ></i>
              <i
                 class="el-icon-remove-outline"
                 type="danger"
                 v-if="index != 0"
                 style="font-size: 30px; color: #f56c6c"
                 @click="del(index)"
                 ></i>
          </el-col>
      </el-row>
      <el-row :gutter="15">
      <el-form-item label="状态:">
        <el-radio-group
          v-model="form.status">
                  <el-radio :label="1">正常</el-radio>
                  <el-radio :label="2">停用</el-radio>
                </el-radio-group>
      </el-form-item>

      <el-form-item label="显示顺序:" prop="sort">
        <el-input-number
          :min="0"
          v-model="form.sort"
          placeholder="请输入显示顺序"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>
      </el-row>


          </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import uploadImage from '@/components/uploadImage'

export default {
  name: 'StandardEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {uploadImage},
  data() {
    return {
      // 表单数据
      form: Object.assign({status:1}, this.data),
      staunit:[
        {label:'个',value:'个'},
        {label:'次/分钟',value:'time'},
        {label:'毫升',value:'ml'},
        {label:'厘米',value:'cm'},
        {label:'秒',value:'s'},
        {label:'千克',value:'kg'},
        {label:'千克/米^2',value:'kg/m^2'},
      ],
      stalevel:[],
      // 等级数据1
      stalevel1: [
        {label:'优秀',value:'1'},
        {label:'良好',value:'2'},
        {label:'及格',value:'3'},
        {label:'不及格',value:'0'}
      ],
      // 等级数据2
      stalevel2: [
        {label:'偏瘦',value:'1'},
        {label:'正常',value:'2'},
        {label:'超重',value:'3'},
        {label:'肥胖',value:'0'}
      ],
      // 表单验证规则
      rules: {
              name: [
          {required: true, message: '请输入考核标准名称', trigger: 'blur'}
        ],
                  organization: [
          {required: true, message: '请输入所属组织', trigger: 'blur'}
        ],
                  project: [
          {required: true, message: '请输入所属项目', trigger: 'blur'}
        ],
                  standard: [
          {required: true, message: '请输入考核标准', trigger: 'blur'}
        ],

        status: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ],
                          sort: [
          {required: true, message: '请输入显示顺序', trigger: 'blur'}
        ],
                  note: [
          {required: true, message: '请输入备注', trigger: 'blur'}
        ],
            },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      // 学院列表
      schoolList: [],
      // 项目列表
      projectList: [],
      // 父级院系列表
      // departmentsList: [],
      ruleForms:[{sex:'',conditions: '',target:['',''],unit:'',level:'',score:''}]
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.ruleForms = this.form.standard;
        console.log(this.ruleForms);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.ruleForms = [{sex:'',conditions: '',target:['',''],unit:'',level:'',score:''}];
        this.isUpdate = false;
      }
    }
  },
  mounted() {
    // 获取学院列表
    this.getSchoolList();
    this.getProjectList();
    // 获取父级院系列表
    // this.getDepartmentsList();
    // 获取班级列表
    // this.getClassList();
  },
  methods: {
    addItem() {
        // this.i += 1;
        this.ruleForms.push({sex:'',conditions: '',target:['',''],unit:'',level:'',score:''});
    },
    // 删除
    del(index) {
        this.ruleForms.splice(index, 1);
    },
    // 修改单位
    changelevel(index){
      if (index=='kg/m^2') {
        this.stalevel = this.stalevel2;
      }else{
        this.stalevel = this.stalevel1
      }
    },
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.form.standard = JSON.stringify(this.ruleForms);
          this.$http.post('/standard/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
    /**
     * 获取组织列表
     */
    getSchoolList() {
      if(this.form.organization){
        this.getProjectList();
      }
      this.$http.get('/school/getSchoolList').then(res => {
        if (res.data.code === 0) {
          this.schoolList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },

    /**
     * 获取运动项目列表
     */
    getProjectList() {
      if(this.form.organization){
        // this.form.project = null;
        this.$forceUpdate();
      }
      this.$http.post('/project/getProjectList',{oid:this.form.organization}).then(res => {
        if (res.data.code === 0) {
          this.projectList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },

    /**
     * 获取院系列表
     */
    // getDepartmentsList() {
    //   if(this.form.organization){
    //     this.form.superior = null;
    //     this.$forceUpdate();
    //   }
    //   this.$http.post('/departments/getDepartmentsList',{sid:this.form.organization}).then(res => {
    //     if (res.data.code === 0) {
    //       this.departmentsList = res.data.data;
    //     } else {
    //       this.$message.error(res.data.msg);
    //     }
    //   }).catch(e => {
    //     this.$message.error(e.message);
    //   });
    // },

    /**
     * 获取班级列表
     */
    getClassList() {
      if(this.form.superior){
        this.form.class = null;
        this.$forceUpdate();
      }
      this.$http.post('/clss/getClassList',{yid:this.form.superior}).then(res => {
        if (res.data.code === 0) {
          this.classList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
  }
}
</script>

<style scoped>
</style>
